package com.wx.mp.dao.impl;

import com.wx.mp.dao.TCarUserRegisterDao;
import com.wx.mp.domain.TCarUserRegister;
import com.wx.mp.domain.TCarUserRegisterExample;
import com.wx.mp.mapper.TCarUserRegisterMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * Copyright: Copyright (c) 2017  zteits
 *
 * @ClassName: TCarUserRegisterDaoImpl.java
 * @Description:
 * @version: v1.0.0
 * @author: wangfs
 * @date: 2019-01-11 10:55
 * Modification History:
 * Date             Author          Version            Description
 * ---------------------------------------------------------*
 * 2019-01-11     wangfs              v1.0.0               创建
 */
@Component
public class TCarUserRegisterDaoImpl implements TCarUserRegisterDao {

    private static final Logger logger = LoggerFactory.getLogger(TCarUserRegisterDaoImpl.class);
    @Autowired
    private TCarUserRegisterMapper carUserRegisterMapper;
    /**
     * 保存用户扫码关注的openId.<br/>
     * @param openId
     * @return
     */
    @Override
    public int saveUserRegister(String openId) {
        logger.info("begin 保存用户扫码关注信息,入参={}",openId);
        int insertCount = 0;
        if(!StringUtils.isEmpty(openId)){
            TCarUserRegister record = new TCarUserRegister();
            record.setOpenId(openId);
            record.setCreateDate(new Date());
            record.setRemarks("用户扫码关");
            insertCount = carUserRegisterMapper.insertSelective(record);
        }
        logger.info("end 保存用户扫码关注信息,插入={}条.",insertCount);
        return insertCount;
    }

    /**
     * 取消关注事件openId.<br/>
     * @param openId
     * @return
     */
    @Override
    public int cancelFollowEnvent(String openId) {
        logger.info("begin 取消关注信息用户关注,入参={}",openId);
        int updateCount = 0;
        TCarUserRegister record = new TCarUserRegister();
        record.setDataState(0);
        record.setModifyDate(new Date());
        record.setRemarks("用户取消关注");
        TCarUserRegisterExample example = new TCarUserRegisterExample();
        example.createCriteria().andOpenIdEqualTo(openId)
                .andDataStateEqualTo(1);
        updateCount = carUserRegisterMapper.updateByExampleSelective(record,example);

        logger.info("end 取消关注信息用户关注,插入={}条.",updateCount);
        return 0;
    }
}
